from dataclasses import dataclass, fields, asdict

from FileHandlers.JsonFile import JReadSave, JWriteSave
from data.config.DefaultConfig import player_data
from entiys.map.MapSaveManager import MapSaveManager
from entiys.npc.NPCLoader import NPCLoader
from entiys.npc.NPCSave import NPCSave


@dataclass
class SaveAttribute:
    """
    通过saveName加载与其相关的所有数据
    """

    saveName: str

    def npc(self):
        return NPCLoader(saveName=self.saveName)

    def npc_save(self, json):
        JWriteSave(saveName=self.saveName, data=json, filename='npc')

    def getPlaydataJson(self) -> dict:
        """
        获取用户属性变量
        :return:
        """
        return JReadSave(saveName=self.saveName, filename="player-data")

    def setPlaydataJson(self, _dict):
        """
        设置用户属性变量
        :param _dict:
        :return:
        """
        if isinstance(_dict, dict):
            JWriteSave(saveName=self.saveName, filename="player-data", data=_dict)
        elif isinstance(_dict, player_data):
            JWriteSave(saveName=self.saveName, filename="player-data", data=asdict(_dict))

    def npcJSON(self):
        """
        加载npc的所有数据
        :return:
        """
        return self.npc().npcJson

    def mapJSON(self):
        """
        加载存档的地图数据
        :return:
        """
        self.mapSaveManager = MapSaveManager(saveName=self.saveName)
        self.mapSaveManager.loadSave()

        return self.mapSaveManager

    def saveMe(self):
        """
        存档
        :return:
        """
        self.mapSaveManager.Save()  # 地图的存档
        NPCSave(self.saveName).save2json()  # npc的存档
